<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>摇一摇测试</title>
  <link rel="stylesheet" href="./css/common.css">
  <style>
  input,button{
    height: 10vw;
    width: 92vw;
    border: 1px solid #ccc;
    border-radius: 3px;
    margin: 0 auto 10vw;
    display: block;
  }
  body,html{
    height: 100%;
  }
  #addname{
    position: absolute;
    top: 0;left: 0;
    width: 100%;
    height: 100%;
    background: #fff;
  }
  #box{
    width: 50vw;
    height: 50vw;
    line-height: 50vw;
    text-align: center;
    background: rgb(211, 59, 59);
    font-size: 20vw;
    color: #fff;
    font-weight: bold;
    margin: 0 auto;
    border-radius: 50%;
  }
  #penson_name{
    text-align: center;
    margin: 10vw auto 0;
    font-size: 5vw;
  }
  </style>
</head>
<body>
  <div id="addname">
    <div style="height: 10vw;"></div>
    <input type="text" id="name" placeholder="填写昵称">
    <button onclick="page.addPenson()">开始</button>
  </div>
  <div style="height: 30vw;"></div>
  <div id="box">0</div>
  <div id="penson_name"></div>
  
  <script src="./js/jquery.js"></script>
  <script src="./js/init.js"></script>
  <script>
  var page = {
    tempx:0,
    tempy:0,
    tempz:0,
    level:50,
    h_id:'',
    p_id:'',
    name:document.getElementById('name'),
    addname:document.getElementById('addname'),
    penson_name:document.getElementById('penson_name'),
    init:function(){
      this.h_id = decodeURIComponent(utils.getUrlParam('h_id'));
    },
    deviceMotionHandler:function(){
      if(window.DeviceMotionEvent){
        window.addEventListener('devicemotion', function(eventData) {
          var a = eventData.accelerationIncludingGravity;
          if(Math.abs(a.x-page.tempx)>page.level||
            Math.abs(a.y-page.tempy)>page.level||
            Math.abs(a.z-page.tempz)>page.level)
          {
            page.addPensonCount();
          }
          page.tempx = a.x;
          page.tempy = a.y;
          page.tempz = a.z;
        }, false);
      }else{
        alert('本设备不支持devicemotion事件');
      }
    },
    addPenson:function(){
      if (!this.name.value) {
        alert('请填写姓名')
        return;
      }
      if (!this.h_id) {
        alert('缺少参数')
        return;
      }
      utils.ajax({
        url:API.addPenson,
        data:{h_id:this.h_id,name:this.name.value},
        success:function(data){
          if (data.status==1) {
            page.p_id = data.data.p_id;
            page.penson_name.innerText = data.data.name;
            page.deviceMotionHandler();
            page.addname.style.display = 'none';
          } else {
            alert(data.message)
          }
        }
      })
    },
    addPensonCount:function(){
      if (!this.h_id) {
        alert('缺少参数')
        return;
      }
      if (!this.h_id) {
        alert('请填写昵称')
        return;
      }
      utils.ajax({
        url:API.addPensonCount,
        data:{h_id:this.h_id,p_id:this.p_id},
        success:function(data){
          if (data.status==1) {
            document.getElementById('box').innerHTML = data.data.count;
          }
        }
      })
    }
  }

  window.onload = function(){
    page.init();
  }
  </script>
</body>
</html>